package com.hr.util;

import com.hr.model.Company;
import com.hr.model.Position;
import com.hr.model.User;
import com.hr.service.WantedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

public class AuthUtil
{
    //检查是否为公司的hr
    public static boolean checkByCompany(Company company)
    {
        for(Position position: ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getArchive().getPositions())
        {
            if(position.getPrivilege()=="管理"&&position.getCompany().getId()==company.getId())
            {
                return true;
            }
        }
        return false;
    }

    //检查是否为公司的hr
    public static boolean checkByCId(int cid)
    {
        for(Position position: ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getArchive().getPositions())
        {
            if(position.getPrivilege()==null)
                continue;
            if(position.getPrivilege().equals("管理")&&position.getCompany().getId()==cid)
            {
                return true;
            }
        }
        return false;
    }

    //取得当前用户
    public static User getCurUser()
    {
        return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }
}
